import logging
import requests
from api.login_api import Mepsking_Login
from common.utils import ordertoken
from config import Base_Host
class Order:
    def submitorder(self,json_dict,token):
        json_dict["orderToken"]=ordertoken()
        url=Base_Host+'/api/meps/shopOrder/submitOrder'
        header_dict={'accessToken':token}
        resp=requests.post(url=url,headers=header_dict,json=json_dict)
        logging.info('提交订单接口返回的数据为：{}'.format(resp.json()))
        return resp
    def closeOrder(self,json_dict,token):
        resp=self.submitorder(json_dict,token)
        if resp.json().get('result')==None:
            logging.info('取消订单接口返回的数据为：{}'.format(resp.json()))
            return resp
        else:
            orderSn=resp.json().get('result').get('orderSn')
            url=Base_Host+'/api/meps/shopOrder/closeOrder'
            json_dict={"cancelReason":"I don't want to buy (don't need/don't like etc.) the item(s) any","orderSn":orderSn,"remark":""}
            header_dict = {'accessToken': token}
            resp=requests.post(url=url,json=json_dict,headers=header_dict)
            logging.info('取消订单接口返回的数据为：{}'.format(resp.json()))
            return resp
if __name__ == '__main__':
    resp=Mepsking_Login()
    login_dict={"account": "2524726232@qq.com", "password": "8c730e7f3dac741443cd3a57cdb7fd8f", "sourceFrom": "PC"}
    token=resp.token(login_dict)
    pay_test=Order()
    a={"carryWay":"ORDINARY","freightAmount":10,"isFlag":"yes","note":"",
						"payPrice":5,"receiverInfo": {"address":{"detailAddress":"12121212","orderAddressCodeDto":
						{"countryName":"1556489637361520641","oneRegionName":"1556489637554458626","twoRegionName"
						:"1556489637747396609"}, "orderAddressNameDto": {"countryNameLocal":"China","oneRegionNameLocal"
						:"Anhui","twoRegionNameLocal":"Anqing"},"zipCode":"212"},"receiverName":"wu@_@jinbo",
							"receiverPhone":"86,212121212121"},
						"orderItems":[{"goodsSkuId":"1639522027916689410","skuQuantity":1}]}
    pay_test.closeOrder(a, token)



